<template>
  <Form
    ref="loginForm"
    :model="form"
    :rules="rules"
    @keydown.enter.native="handleSubmit"
  >
    <FormItem prop="username">
      <Input v-model="form.username" placeholder="请输入用户名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password" v-model="form.password" placeholder="请输入密码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="code">
      <Input
        type="text"
        class="codet"
        v-model="form.code"
        placeholder="请输入验证码"
      >
        <span slot="prepend">
          <Icon :size="14" type="ios-image"></Icon>
        </span>
        <span
          class="code"
          slot="append"
          @click="changeCode"
          v-html="svg"
        ></span>
      </Input>
    </FormItem>
    <FormItem>
      <Button @click="handleSubmit" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
// import { login } from "@/api/user";
import uuid from 'uuid/v4'
import {
  getCode
} from '@/api/login'
export default {
  name: 'LoginForm',
  props: {
    usernameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { type: 'email', message: '账号或密码错误', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { type: 'string', min: 4, message: '密码不能小于4位', trigger: 'blur' }
        ]
      }
    },
    codeRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { type: 'string', len: 4, message: '验证码错误', trigger: 'change' }
        ]
      }
    }
  },
  data () {
    return {
      svg: '',
      sid: '',
      form: {
        username: '',
        password: '',
        code: '',
      }
    }
  },
  mounted () {
    let sid = ''
    if (localStorage.getItem('sid')) {
      sid = localStorage.getItem('sid')
    } else {
      sid = uuid()
      localStorage.setItem('sid', sid)
    }
    this.$store.commit('setSid', sid)
    this._getCode()
  },
  computed: {
    rules () {
      return {
        username: this.usernameRules,
        password: this.passwordRules,
        code: this.codeRules,
        sid: this.sid
      }
    }
  },
  methods: {
    changeCode () {
      this._getCode()
    },
    _getCode () {
      let sid = this.$store.state.sid
      getCode(sid).then((res) => {
        // const obj = res.data
        if (res.code === 200) {
          this.svg = res.data
        }
      })
    },
    handleSubmit () {
      // localStorage.setItem("token", res.data.token)
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            username: this.form.username,
            password: this.form.password,
            code: this.form.code,
            sid: this.$store.state.sid
          })
        }
      })

    }
  }
}
</script>
<style  lang='scss' >
.ivu-input-group-append {
  padding: 0;
}
.code {
  display: inline-block;
  padding: 0;
  height: 28px;
  overflow: hidden;
  svg {
    width: 120px;
    position: relative;
    top: -5px;
    height: 28px;
  }
}
</style>